# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : arm.py
# Date      : 2018/11/8
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------


__author__ = "lin lingwei"

import maya.cmds as mc

import configs.constants.attribute_constant as ac
import configs.constants.nodeType_constant as nc
import configs.constants.suffix_constant as sc
import configs.constants.body_constant as bc
import configs.constants.global_constant as gc

import scripts.commonCmds as con
import scripts.curveCmds as crc

import rig.body.bCore as bCore
import rig.jointTool as jt
import rig.sample as sample
reload(bCore)

import rig.rpIkhandle as rik
reload(rik)
reload(gc)
reload(sc)
reload(ac)
reload(bc)
reload(sample)


class Arm(bCore.BodyCore):
    
    def __init__(self,localMake,localPrefix="",localTag=bc.gArm):
        super(Arm,self).__init__(localTag=localTag,
                                 localMake=localMake,
                                 localPrefix=localPrefix)
        
        self.seq = [bc.gShoulder, bc.gArm, bc.gForeArm, bc.gWrist, bc.gHand, bc.gHandEnd, bc.gPolar]
        self.localSeq = [bc.gShoulder,bc.gArm,bc.gForeArm,bc.gWrist,bc.gHand,bc.gHandEnd]
        
        self.root_flw = None
        self.handle_flw = None

    def create(self,mirror=False):
        self.localPrefix = gc.gLeft
        if mirror:
            self.localPrefix = gc.gRight
            self.local_colorId = bc.cRight
        self.refresh_localName()
        self.localInit()
        # tmpData = self.parsePartTemplate()

        self.shouler_setting(mirror)
        
        drjot_list = self.create_joint(bc.gDr, self.seq[1:5], mirror=mirror)
        
        aikobj = self.ik_setting(mirror)
        fkjot_list = self.fk_setting(mirror)
        
        self.ikfkSwitch(aikobj.jotlist,fkjot_list,drjot_list)
        mc.connectAttr(con.lAttr(self.localBridge,bc.aIkSwitch),aikobj.localGrp+".v")
        fh_tag = fkjot_list[0].replace(sc.sJot, sc.sCtr)
        mc.connectAttr(con.lAttr(self.localBridge,bc.aFkSwitch),fh_tag+"_Zero.v")

        mc.parent(drjot_list[0],self.localPrefixGrp)
        
        self.ikfk_ctr(drjot_list[-2])
        
        return
    def ikfk_ctr(self,inobj):
        ikfk_ctr = crc.objToControl(inobj,name=con.lname(self.localName,ac.ikFk_switch),
                                    colorId=self.local_colorId,ctrlShape="cube",scale=0.5)
        mc.setAttr(ikfk_ctr+".r",0,0,0)
        mc.setAttr(ikfk_ctr+".s",1,1,1)
        mc.parent(ikfk_ctr,self.localGrp)
        ikfk_ctr_grp = con.emptyGroup(ikfk_ctr)
        mc.setAttr(ikfk_ctr_grp[2]+".ty",0.5)
        mc.parentConstraint(inobj,ikfk_ctr_grp[1],mo=True)
        con.lockHideAttr(ikfk_ctr)
        con.transferAttr(self.localBridge,ikfk_ctr,(bc.aIkFkSwitch,))
        return ikfk_ctr
    
    def ik_setting(self,mirror=False,root=bc.gArm,handle=bc.gWrist):
        lname_ik = con.lname(self.localName,bc.gIk)
        tmpData = self.parsePartTemplate()
        # armPolar = crc.objToControl(tmpData[bc.gForeArmPolar], con.lname(self.localName,bc.gPolar))
        
        # ik setting
        ikjot_list = self.create_joint(bc.gIk, self.seq[1:4], mirror=mirror)
        mc.hide(ikjot_list[0])
        
        aikobj = rik.RpIkhandle(con.lname(lname_ik))
        aikobj.create(ikjot_list[0],ikjot_list[2])
        aikobj.jot_root()
        aikobj.stretch()
        aikobj.auto_polar()
        aikobj.lockPolar()
        auto_flw = aikobj.polar_follow()
        
        # --- control
        arm_ctr = crc.objToControl(tmpData[handle],name=con.lname(lname_ik),
                                   colorId=self.local_colorId,
                                   ctrlShape="cube",mirror=mirror)
        # arm_loc = con.objToLocator(arm_ctr,name=con.lname(self.localName,"output"))
        mc.parent(arm_ctr,aikobj.localGrp)
        mc.parent(aikobj.ikhandle_loc,arm_ctr)
        arm_grp = con.emptyGroup(arm_ctr)
        mc.orientConstraint(arm_ctr,ikjot_list[-1],mo=True)
        con.lockHideAttr(arm_ctr,["sx","sy","sz","v"])
        
        polar_ctr = crc.objToControl(tmpData[bc.gPolar],name=con.lname(lname_ik,bc.gPolar),
                                     colorId=self.local_colorId,
                                     ctrlShape="sphere",mirror=mirror)
        mc.parent(polar_ctr,aikobj.localGrp)
        mc.parent(aikobj.polarObj,polar_ctr)
        polar_grp = con.emptyGroup(polar_ctr)
        mc.setAttr(aikobj.polarObj+".tz",0)
        con.lockHideAttr(polar_ctr,ignore=["tx","ty","tz"])

        
        armRoot_ctr = crc.objToControl(tmpData[root],name=con.lname(lname_ik,bc.gRoot),
                                       colorId=self.local_colorId,
                                     ctrlShape="sphere",mirror=mirror)
        mc.delete(mc.aimConstraint(arm_ctr, armRoot_ctr, mo=False,
                                   aimVector=[1, 0, 0], upVector=[0, 0, -1], worldUpType="object",
                                   worldUpObject=polar_ctr))
        mc.parent(armRoot_ctr,aikobj.localGrp)
        mc.parent(aikobj.jotRoot,armRoot_ctr)
        armRoot_grp = con.emptyGroup(armRoot_ctr)
        con.lockHideAttr(armRoot_ctr,ignore=["tx","ty","tz"])

        mc.parent(aikobj.localGrp,self.localPrefixGrp)
        
        hobj = self.followHook(arm_grp[1],fowAttr=(ac.local,bc.gShoulder))
        mc.parent(hobj[ac.local],self.transfer)
        mc.parent(hobj[bc.gRoot],arm_grp[0])
        mc.connectAttr(hobj[sc.sBridge]+".t",arm_grp[1]+".t")
        mc.connectAttr(hobj[sc.sBridge]+".r",arm_grp[1]+".r")
        
        mc.parent(hobj[bc.gShoulder], self.root_flw)
        mc.parentConstraint(self.root_flw, armRoot_grp[1], mo=True)
        
        mc.addAttr(polar_ctr,ln="auto_align",at="long",dv=1,min=0,max=1,k=True)
        polar_pat= mc.pointConstraint(auto_flw,polar_grp[1],mo=True)[0]
        mc.connectAttr(con.lAttr(polar_ctr,"auto_align"),polar_pat+'.%sW%s' % (auto_flw,0))
        
        con.transferAttr(hobj[sc.sBridge],arm_ctr,(bc.aFollow,))
        con.transferAttr(aikobj.localBridge,arm_ctr,(ac.stretch_switch,))
        con.transferAttr(aikobj.localBridge,polar_ctr,(ac.lock_polar,))

        con.hideJoint(aikobj.jotlist)
        
        return aikobj
    
    def fk_setting(self,mirror=False,index=[1,4]):
        lname_ik = con.lname(self.localName,bc.gFk)

        # tmpData = self.parsePartTemplate()
        # armPolar = crc.objToControl(tmpData[bc.gForeArmPolar], con.lname(self.localName,bc.gPolar))
    
        # ik setting
        fkjot_list = self.create_joint(bc.gFk, self.seq[index[0]:index[1]], )
        jot_list = sample.selectJointChain(fkjot_list,lname_ik,mirror=mirror,colorId=self.local_colorId)
        mc.delete(fkjot_list)
        mc.hide(jot_list)
        fh_tag = jot_list[0].replace(sc.sJot,sc.sCtr)
    
        mc.parent(fh_tag+"_Zero",self.localPrefixGrp)
   
        hobj = self.followHook(fh_tag+"_Con",fowAttr=(ac.local,bc.gShoulder),onlyOrient=1)
        mc.parent(hobj[ac.local],self.transfer)
        mc.parent(hobj[bc.gRoot],fh_tag+"_Zero")
        mc.connectAttr(hobj[sc.sBridge]+".t",fh_tag+"_Con.t")
        mc.connectAttr(hobj[sc.sBridge]+".r",fh_tag+"_Con.r")
        
        mc.parent(hobj[bc.gShoulder], self.root_flw)
        con.transferAttr(hobj[sc.sBridge],fh_tag,(bc.aFollow,))

        return jot_list

    def shouler_setting(self,mirror=False,tag=bc.gShoulder):
        
        shouler_jot = self.create_joint("", [self.seq[0]], mirror=mirror)[0]
        shouler_grp = con.emptyGroup(shouler_jot,["Zero"])[0]
        tmpData = self.parsePartTemplate()
        ctr = crc.objToControl(tmpData[tag],name=con.lname(self.localName,tag),
                               colorId=self.local_colorId,
                               mirror=mirror)
        mc.parent(shouler_grp,ctr)
        mc.parent(ctr,self.localGrp)
        con.emptyGroup(ctr)
        self.root_flw = con.objToType(shouler_grp,
                                      name=con.lname(self.localName,tag,"flw"))
        if mirror: con.localMirror(self.root_flw, "world")
        mc.parent(self.root_flw, shouler_grp)
        
        con.lockHideAttr(ctr,["sx","sy","sz","v"])

        return shouler_jot

if __name__ == '__main__':
    pass